package com.ztc.Chess;

import java.util.Scanner;

/**
 * Created by ztc on 15-10-24.
 */
public class Gogame {
    Scanner in;     //=new Scanner(System.in);
    int X=7,Y=7,oldX,oldY;
    Board board=new Board();
    public String input()
    {
        in=new Scanner(System.in);
        return in.nextLine();
    }
    public boolean isValid(String xy)
    {
        String[] x_y=xy.split(",");
        try {
            X = Integer.parseInt(x_y[0]) - 1;
            Y = Integer.parseInt(x_y[1]) - 1;
        }catch (NumberFormatException e)
        {
            board.displayBoard();
            System.out.println("请以 数字，数字 的格式输入！");
            return false;
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            board.displayBoard();
            System.out.println("请以 数字，数字 的格式输入！");
            return false;
        }
        if(X>board.SIZE-1||X<0||Y<0||Y>board.SIZE-1)
        {
            board.displayBoard();
            System.out.println("1<=（X，Y）<="+board.SIZE);
            return false;
        }
        if(board.getBoard()[X][Y]!="十") {
            board.displayBoard();
            System.out.println("该位置已有棋子!");
            return false;
        }
        return true;
    }

    public boolean isReplay(String chessman)
    {
        board.displayBoard();
        String message=chessman.equals(Chessman.BLACK.getChessman())?"恭喜，YOU WIN！\n":"YOU LOSE！";
        System.out.println(message+"在来一局？（y/n)");
        in=new Scanner(System.in);
        String br=in.nextLine();
        if(br.equals("y"))
            return true;
        return false;
    }

    public boolean isWon(int x,int y,String ico)
    {
        int num=1;
        for(int i=1;i<5;i++)   //纵向
        {

            if((x+i)<Board.SIZE&&board.board[x+i][y].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {

            if((x-i)>=0&&board.board[x-i][y].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("纵"+num);
        if(num==5)
            return true;
        else
            num=1;
        for(int i=1;i<5;i++)   //横向
        {
            if((y+i)<Board.SIZE&&board.board[x][y+i].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {
            if((y-i)>=0&&board.board[x][y-i].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("横"+num);
        if(num==5)
            return true;
        else
            num=1;
        for(int i=1;i<5;i++)   //"\"向
        {
            if((x+i)<Board.SIZE&&(y+i)<Board.SIZE&&board.board[x+i][y+i].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {
            if((x-i)>=0&&(y-i)>=0&&board.board[x-i][y-i].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("\\"+num);
        if(num==5)
            return true;
        else
            num=1;
        for(int i=1;i<5;i++)   //"/"向
        {
            if((x+i)<Board.SIZE&&(y-i)>=0&&board.board[x+i][y-i].equals(ico))
                num++;
            else
                break;
        }
        for(int i=1;i<5;i++)
        {
            if((x-i)>=0&&(y+i)<Board.SIZE&&board.board[x-i][y+i].equals(ico))
                num++;
            else
                break;
        }
        System.out.println("/"+num);
        if(num==5)
            return true;
        else
            num=1;

        return false;
    }

    public int[] computerDo(int x,int y,int oldx,int oldy)
    {
        /*
        int poxX=(int)(Math.random()*Board.SIZE-1);
        int poxY=(int)(Math.random()*Board.SIZE-1);
        while(board.board[poxX][poxY]!="十")
        {
            poxX=(int)(Math.random()*Board.SIZE-1);
            poxY=(int)(Math.random()*Board.SIZE-1);
        }
        int[] a={poxX,poxY};
        return a;
        */

        int poxX=13,poxY=13,i=1;
        while(poxX>0&&poxY>0&&poxX<Board.SIZE&&poxY<Board.SIZE&&board.board[poxX][poxY]!="十")
        {
            if(x-i>=0&&x+i<Board.SIZE&&y-i>=0&&y+i<Board.SIZE) {
                if (x > oldx && y > oldy) {
                    poxX = x + i;
                    poxY = x + i;
                } else if (x > oldx && y < oldy) {
                    poxX = x + i;
                    poxY = y - i;
                } else if (x < oldx && y > oldy) {
                    poxX = x - i;
                    poxY = y + i;
                } else if (x < oldx && y < oldy) {
                    poxX = x - i;
                    poxY = y - i;
                } else if (x == oldx && y < oldy) {
                    poxX = x;
                    poxY = y - i;
                } else if (x == oldx && y > oldy) {
                    poxX = x;
                    poxY = y + i;
                } else if (x < oldx && y == oldy) {
                    poxX = x - i;
                    poxY = y;
                } else {
                    poxX = x + 1;
                    poxY = y;
                }
            }else
            {
                poxX=(int)(Math.random()*Board.SIZE-1);
                poxY=(int)(Math.random()*Board.SIZE-1);
            }
            i++;
        }

        int[] a={poxX,poxY};
        return a;
    }

    public void start()
    {
        board.initBoard();
        while(true)
        {
            board.displayBoard();
            boolean sign;
            oldX=X;
            oldY=Y;
            do
             {
                sign=isValid(input());
             }while(!sign);
            board.board[X][Y]=Chessman.BLACK.getChessman();
            if(isWon(X,Y,Chessman.BLACK.getChessman()))
             {
                 if(isReplay(Chessman.BLACK.getChessman()))
                        start();
                 else
                        break;
             }
            int[] CS=computerDo(X,Y,oldX,oldY);
            board.board[CS[0]][CS[1]]=Chessman.WRITE.getChessman();
            if(isWon(X, Y, Chessman.WRITE.getChessman()))
            {
                if(isReplay(Chessman.WRITE.getChessman()))
                    start();
                else
                    break;
            }
        }

    }

}
